﻿// CPopDelay.cpp: 实现文件
//

#include "pch.h"
#include "DntDemo.h"
#include "afxdialogex.h"
#include "CPopDelay.h"
#include "FwbLog.h"
#include "CTabAudioKc3x.h"
#define TAG	"CPopDelay"

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 标准C语言函数 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CPopDelay* mCPopDelay = NULL;
static void changRegCallback() {
	if (mCPopDelay != NULL) {
		mCPopDelay->ShowRegister();
	}
}

int CPopDelay::ShowSlider(uint8_t value, int slider, int edit) {			// 显示数值
	CString text;
	text.Format(L"%d", value);
	((CStatic*)GetDlgItem(edit))->SetWindowText(text);
	return value;
}

void CPopDelay::SetDelayValue(int slider, int edit) {						// 读取选项的状态值
	int position = ((CSliderCtrl*)GetDlgItem(slider))->GetPos();
	switch (slider) {
		case IDC_DELAY_CE:				// 中置延时
			gDelayValue[2] = position;
			break;
		case IDC_DELAY_SL:				// 环绕左延时
			gDelayValue[4] = position;
			break;
		case IDC_DELAY_SR:				// 环绕右延时
			gDelayValue[5] = position;
			break;
		case IDC_DELAY_BL:				// 后置左延时
			gDelayValue[6] = position;
			break;
		case IDC_DELAY_BR:				// 后置右延时
			gDelayValue[7] = position;
			break;
	}
	ShowSlider(position, slider, edit);
	gSlowTime = 3;
}

void CPopDelay::ShowRegister() {
	int position1 = ShowSlider(mKc3x->DelayTime[2], IDC_DELAY_CE, IDC_DELAY_CE_VALUE);
	int position2 = ShowSlider(mKc3x->DelayTime[4], IDC_DELAY_SL, IDC_DELAY_SL_VALUE);
	int position3 = ShowSlider(mKc3x->DelayTime[5], IDC_DELAY_SR, IDC_DELAY_SR_VALUE);
	int position4 = ShowSlider(mKc3x->DelayTime[6], IDC_DELAY_BL, IDC_DELAY_BL_VALUE);
	int position5 = ShowSlider(mKc3x->DelayTime[7], IDC_DELAY_BR, IDC_DELAY_BR_VALUE);

	//CLOGI(TAG, "ShowRegister AAAA(%02X) (%02X)(%02X)(%02X)(%02X)(%02X)(%02X)(%02X)(%02X)", mKc3x->DelayTime,  mKc3x->DelayTime[0], mKc3x->DelayTime[1], mKc3x->DelayTime[2], mKc3x->DelayTime[3], mKc3x->DelayTime[4], mKc3x->DelayTime[5], mKc3x->DelayTime[6], mKc3x->DelayTime[7]);
	//CLOGI(TAG, "ShowRegister BBBB(%02X) (%02X)(%02X)(%02X)(%02X)(%02X)(%02X)(%02X)(%02X))",  mKc3x->MaxDelayTime, mKc3x->MaxDelayTime[0], mKc3x->MaxDelayTime[1], mKc3x->MaxDelayTime[2], mKc3x->MaxDelayTime[3], mKc3x->MaxDelayTime[4], mKc3x->MaxDelayTime[5], mKc3x->MaxDelayTime[6], mKc3x->MaxDelayTime[7]);
	if (gSlowTime == 0) {		// 拉动滑块松手后才SetPos数值，防止拉动滑块反复跳动
		((CSliderCtrl*)GetDlgItem(IDC_DELAY_CE))->SetPos(position1);
		((CSliderCtrl*)GetDlgItem(IDC_DELAY_SL))->SetPos(position2);
		((CSliderCtrl*)GetDlgItem(IDC_DELAY_SR))->SetPos(position3);
		((CSliderCtrl*)GetDlgItem(IDC_DELAY_BL))->SetPos(position4);
		((CSliderCtrl*)GetDlgItem(IDC_DELAY_BR))->SetPos(position5);
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// C++语言对话框函数 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// CPopDelay 对话框

IMPLEMENT_DYNAMIC(CPopDelay, CDialogEx)

CPopDelay::CPopDelay(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_POP_DELAY, pParent){
}

CPopDelay::~CPopDelay(){
}

void CPopDelay::DoDataExchange(CDataExchange* pDX){
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CPopDelay, CDialogEx)
	ON_WM_TIMER()
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_DELAY_CE, &CPopDelay::OnNMCustomdrawDelayCe)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_DELAY_SL, &CPopDelay::OnNMCustomdrawDelaySl)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_DELAY_SR, &CPopDelay::OnNMCustomdrawDelaySr)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_DELAY_BL, &CPopDelay::OnNMCustomdrawDelayBl)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_DELAY_BR, &CPopDelay::OnNMCustomdrawDelayBr)
END_MESSAGE_MAP()


// CPopDelay 消息处理程序

BOOL CPopDelay::OnInitDialog(){
	CDialogEx::OnInitDialog();
	// TODO:  在此添加额外的初始化

	((CSliderCtrl*)GetDlgItem(IDC_DELAY_CE))->SetRange(0, 15);	// 设置最小值和最大值
	((CSliderCtrl*)GetDlgItem(IDC_DELAY_SL))->SetRange(0, 40);	// 设置最小值和最大值 
	((CSliderCtrl*)GetDlgItem(IDC_DELAY_SR))->SetRange(0, 40);	// 设置最小值和最大值
	((CSliderCtrl*)GetDlgItem(IDC_DELAY_BL))->SetRange(0, 40);	// 设置最小值和最大值
	((CSliderCtrl*)GetDlgItem(IDC_DELAY_BR))->SetRange(0, 40);	// 设置最小值和最大值

	memset(gDelayValue, 0, sizeof(gDelayValue));
	mCPopDelay = this;
	isPopKc3xRegi = false;
	gSlowTime = 0;
	ShowRegister();
	memcpy(gDelayValue, &mKc3x->DelayTime, sizeof(gDelayValue));		// 复制&mKc3x->DelayTime的数据到gDelayValue中

	TestChangRegCallback(changRegCallback);
	SetTimer(1, 300, NULL);
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}


void CPopDelay::OnTimer(UINT_PTR nIDEvent) {	
	if (memcmp(&mKc3x->DelayTime, gDelayValue, sizeof(gDelayValue))) {		// 如果判断&mKc3x->DelayTime与gDelayValue数据不同
		//CLOGI(TAG, "OnTimer 数据不同 AA (%02X)(%02X)", &mKc3x->DelayTime, gDelayValue);
		memcpy(&mKc3x->DelayTime, gDelayValue, sizeof(gDelayValue));		// 把数据gDelayValue复制到&mKc3x->DelayTime中
		isPopKc3xRegi = true;												// 允许写入寄存器的标志
	}

	if (isPopKc3xRegi) {
		isPopKc3xRegi = false;
		uint8_t save[8];
		memcpy(save, gDelayValue, sizeof(gDelayValue));			// 把gDelayValue复制到save中，防止拉动滑块过程中数值反复跳动
		ShowRegister();
		memcpy(gDelayValue, save, sizeof(gDelayValue));			// 把save复制到gDelayValue中，防止拉动滑块过程中数值反复跳动
		DntRegWrite();											// 写寄存器
	}
	
	if (gSlowTime) {
		--gSlowTime;
	}
	CDialogEx::OnTimer(nIDEvent);
}

void CPopDelay::OnNMCustomdrawDelayCe(NMHDR* pNMHDR, LRESULT* pResult){		// 中置延时
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	SetDelayValue(IDC_DELAY_CE, IDC_DELAY_CE_VALUE);
	*pResult = 0;
}

void CPopDelay::OnNMCustomdrawDelaySl(NMHDR* pNMHDR, LRESULT* pResult){		// 环绕左延时
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	SetDelayValue(IDC_DELAY_SL, IDC_DELAY_SL_VALUE);
	*pResult = 0;
}

void CPopDelay::OnNMCustomdrawDelaySr(NMHDR* pNMHDR, LRESULT* pResult){		// 环绕右延时
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	SetDelayValue(IDC_DELAY_SR, IDC_DELAY_SR_VALUE);
	*pResult = 0;
}

void CPopDelay::OnNMCustomdrawDelayBl(NMHDR* pNMHDR, LRESULT* pResult){		// 后置左延时
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	SetDelayValue(IDC_DELAY_BL, IDC_DELAY_BL_VALUE);
	*pResult = 0;
}

void CPopDelay::OnNMCustomdrawDelayBr(NMHDR* pNMHDR, LRESULT* pResult){		// 后置右延时
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	SetDelayValue(IDC_DELAY_BR, IDC_DELAY_BR_VALUE);
	*pResult = 0;
}
